/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.knolle.campanias.util;

import org.knolle.campanias.util.SeleccionLote;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

    
/**
 *
 * @author marcelo
 */
public  abstract class AbstractLotesTransfer  extends TransferHandler {
    
        
        protected abstract Object[] exportarLotes(JComponent c);
        protected abstract void importarLotes(JComponent c, Object[] datos, Object source );
        protected abstract void importarLotes(JComponent c, Object[] datos );
        protected abstract void cleanup(JComponent c, Boolean remove);

    
        
       

        @Override
        protected Transferable createTransferable(JComponent source) {
            return new SeleccionLote(exportarLotes(source));
        }
        
        @Override
        public int getSourceActions(JComponent c) {
//            System.out.print("Se llama a getSourceActions \n");
            return MOVE;
//            return COPY_OR_MOVE;
        }

        @Override
         public boolean importData(JComponent target, Transferable t) {
             
            //Ver quien es el target
             if (canImport(target, t.getTransferDataFlavors())) {
                try {
                    //Aca se obtiene una instancia de SeleccionLote, 8
                    Object[] datos = (Object[])  t.getTransferData(SeleccionLote.DATA_FLAVOR);                    
                    importarLotes(target, datos);
                    return true;
                } 
                catch (UnsupportedFlavorException ufe) {
                   System.err.print(ufe.getMessage());
                } 
                catch (IOException ioe) {
                   System.err.print(ioe.getMessage());
                }
                }

            return false;
        }
        
        @Override
        protected void exportDone(JComponent source, Transferable data, int action) {

            cleanup(source, action == MOVE);
//            System.out.print("Se llamo al metodo exportDone \n");
        }
        
        @Override
        public boolean canImport(JComponent c, DataFlavor[] flavors) {
            for (int i = 0; i < flavors.length; i++) {
                if (SeleccionLote.DATA_FLAVOR.equals(flavors[i])) {
//                    System.out.print("Puede Import \n");
                    return true;
                }
            }
//            System.out.print("No Puede Importar \n");
            return false;
        }
        
        
    
}

